package StudyCode.summary.myfile;

import java.io.File;
import java.util.Properties;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class FilterMavenStyle {

	/**
	 * @param directory  目录(文件夹)
	 * @param properties Maven项目使用的属性列表
	 */
	public static void doFilenameFiltering(final File directory, final Properties properties) {
		for (final File file : directory.listFiles()) {
			if (file.isDirectory()) {
				doFilenameFiltering(file, properties);
			} else {
				String fileName = file.getName();
				int i = fileName.indexOf("${");
				while (i >= 0) {
					final String property = fileName.substring(i + 2,
							fileName.indexOf('}', i));
					if (properties.containsKey(property)) {
						final String value = properties
								.getProperty(property);
						fileName = fileName.replaceAll("\\$\\{" + property
								+ "\\}", value);
					}
					i = fileName.indexOf("${", i + 1);
				}
				if (!fileName.equals(file.getName())) {
					file.renameTo(new File(file.getParentFile(), fileName));
				}
			}
		}
	}
}
